/**
 * Created by gaoliwei02 on 16/6/21.
 */


var sta_dhcp;
var sta_ip;
var sta_netmask;
var sta_gateway;
var sta_dns;


function dhcp_change() {
    console.log(">dhcp_change");

    if(sta_dhcp.value != null)
    {
        if (sta_dhcp.value == "dhcp_client") {
            sta_ip.disabled = true;
            sta_netmask.disabled = true;
            sta_gateway.disabled = true;
            sta_dns.disabled = true;
        } else if(sta_dhcp.value  == "dhcp_disable"){
            sta_ip.disabled = false;
            sta_netmask.disabled = false;
            sta_gateway.disabled = false;
            sta_dns.disabled = false;

            sta_ip.value = "192.168.1.1";
            sta_netmask.value = "255.255.255.0";
            sta_gateway.value = "192.168.1.1";
            sta_dns.value = "192.168.1.1";
        }
    }
}
function init_dhcp_setting() {

    sta_dhcp = document.getElementById("sta_dhcp");
    sta_ip = document.getElementById("sta_ip");
    sta_netmask = document.getElementById("sta_netmask");
    sta_gateway  = document.getElementById("sta_gateway");
    sta_dns  = document.getElementById("sta_dns");

    console.log(">init done");

    if(sta_dhcp.value == "dhcp_client")
        sta_dhcp.options.selectedIndex = 0;
    else if(sta_dhcp.value  == "dhcp_disable")
        sta_dhcp.options.selectedIndex = 1;
    dhcp_change();
}

function sta_form_apply() {

    if (sta_ssid.value == "") {
        alert("SSID is empty!");
        return;
    }

    if (sta_ssid.value.length > 32) {
        alert("SSID less than or equal to 32!！");
        return;
    }

    if(sta_dhcp.value == "dhcp_disabble" )
    {
        if (!ipAdd(sta_ip.value)) {
            alert("IP invalid!");
            return;
        }

        if (!ipAdd(sta_netmask.value)) {
            alert("netmask invalid!");
            return;
        }

        if (!ipAdd(sta_gateway.value)) {
            alert("gateway invalid!");
            return;
        }
    }

    document.submit();
}

function ipAdd(v) {
    var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
    return re.test(v);
}
